
import java.net.*;

/*This class stores fields and methods used to keep track of and compare machines*/
public class MachineData {

	//fields used by all
	public String name;
	public int port;
	public InetAddress ip; 
	
	//fields written to by Sender
	public long next_time; //next time to which sender needs to hear a ping before taking further action.
	public long last_ping; //last time sender sent a reminder ping to node.
	
	//fields written by receiver
	public byte msg; //transmitted message
	public long time; //also by sender, the time at which the message was received
	
	
	public MachineData(String ln) {
		String[] p = ln.split("\\s");
		String n = p[0];
		
		try {
			ip = InetAddress.getByName(n);
		}
		catch(UnknownHostException e) {
			System.out.println("Error: Couldn't find address.");
		}
		
		port = Integer.parseInt(p[1]);
		name = ip.getHostAddress();
	}
	
	public MachineData(String name, int port, byte b, long time) {
		
		String n = name;
		this.port = port;
		msg = b;
		this.time = time;
		
		try {
			ip = InetAddress.getByName(n);
		}
		catch(UnknownHostException e) {
			System.out.println("Error: Couldn't find address.");
		}
		
		this.name = ip.getHostAddress();
	}
	
	@Override public boolean equals(Object other) {
	    if ( this == other ) return true;

	    if ( !(other instanceof MachineData) ) return false;

	    MachineData m = (MachineData) other; 

	    return
	    	this.name .equals(m.name) && this.port == m.port;
	  }
	
	@Override public String toString() {
		return this.name + " " + this.port;
	}
}
